package com.cloudlead.common.lang.persistence;

import com.cloudlead.common.lang.annotation.Label;

import javax.validation.Valid;
import java.io.Serializable;
import java.util.*;

/**
 * Created by dean on 2015/8/10.
 */
@Label("搜索或条件")
public final class Filter extends AbstractList<Criteria> implements Serializable {

    @Valid
    @Label("或条件")
    private List<Criteria> or;

    public Filter() {
    }

    public Filter(Criteria... or) {
        this(Arrays.asList(or));
    }

    public Filter(List<Criteria> or) {
        if (null == or || or.isEmpty()) {
            throw new IllegalArgumentException(
                    "You have to provide at least one search property to search!");
        }
        this.or = or;
    }

    @Override
    public void add(int index, Criteria element) {
        or.add(index, element);
    }

    @Override
    public Criteria remove(int index) {
        return or.remove(index);
    }

    @Override
    public int size() {
        return or.size();
    }

    @Override
    public Criteria get(int index) {
        return or.get(index);
    }

    @Override
    public Iterator<Criteria> iterator() {
        return or.iterator();
    }

    public List<Criteria> getOr() {
        return or;
    }

    public void setOr(List<Criteria> or) {
        this.or = or;
    }

    public Filter or(Criteria criteria) {
        if (null == or) {
            or = new ArrayList<>();
        }
        or.add(criteria);
        return this;
    }

    @Override
    public String toString() {
        return "Filter{" +
                "or=" + or +
                '}';
    }
}
